﻿using System;
using Framework.MVC;
using Account.Model;
using Account.Model.Behavior;
using Extention.Model;
using Extention.Model.Behavior;
using Identity.Model;
using Framework.MVVM.Model;
using Identity.IService;
using Identity.Service;
using Account.IService;
using Account.Service;

namespace FrontCenter.Models
{
    [ServiceAttribute(typeof(IVerifyCodeService), typeof(VerifyCodeService), "Instance")]
    [ServiceAttribute(typeof(Extention.IService.IActivationService), typeof(Extention.Service.ActivationService), "Instance")]
    [ServiceAttribute(typeof(IAccountService), typeof(AccountService), "Instance")]
    [ServiceAttribute(typeof(IIdentityService), typeof(IdentityService), "Instance")]
    public class ActivationModel : AsyncModelBase, IValidateVerifyCode, IDeleteVerifyCode, IGetAccount, IContainsActivation, IModifyActivation
    {
        #region Filed

        private const string m_result = "result";
        private const string m_error = "error";
        private const string m_errors_verifycode = "verifycode";
        private const string m_errors_account = "account";
        private const string m_errors_contains = "contains";
        private const string m_errors_modify = "modify";

        #endregion Filed

        #region Property

        public string Resource { get; set; }

        #endregion Property

        #region AsyncModelBase

        protected override void OnInit(Type type)
        {
            OnBeginRequest();
            this.ValidateVerifyCode(Resource, string.Empty, string.Empty);
        }

        #endregion AsyncModelBase

        #region IValidateVerifyCode

        public void OnValidateVerifyCodeComplete(string key, bool entry)
        {
            this.DeleteVerifyCode(Resource, string.Empty, string.Empty);
        }

        public void OnValidateVerifyCodeError(string key)
        {
            OnComplete(m_error, m_errors_verifycode);
            OnEndRequest();
        }

        public string VerifyCode { get; set; }

        public string VerifyValue { get; set; }

        #endregion IValidateVerifyCode

        #region IDeleteVerifyCode

        public void OnDeleteVerifyCodeComplete(string key, bool result)
        {
            this.GetAccount(AccountResource,string.Empty, string.Empty);
        }

        public void OnDeleteVerifyCodeError(string key)
        {
            this.GetAccount(AccountResource, string.Empty, string.Empty);
        }

        #endregion IDeleteVerifyCode

        #region IGetAccount

        public string AccountName { get; set; }
        public string AccountResource { get; set; }

        public void OnGetAccountComplete(string key, string code)
        {
            ActivationResource = code;
            this.ContainsActivation(string.Empty, string.Empty);
        }

        public void OnGetAccountError(string key)
        {
            OnComplete(m_error, m_errors_account);
            OnEndRequest();
        }

        #endregion IGetAccount

        #region IContainsActivation

        public string ActivationCode { get; set; }

        public string ActivationResource { get; set; }

        public void OnContainsActivationComplete(string key, bool result)
        {
            this.ModifyActivation(string.Empty, string.Empty);
        }

        public void OnContainsActivationError(string key)
        {
            OnComplete(m_error, m_errors_contains);
            OnEndRequest();
        }

        #endregion IContainsActivation

        #region IModifyActivation

        public void OnModifyActivationComplete(string key, bool result)
        {
            OnComplete(m_result, result);
            OnEndRequest();
        }

        public void OnModifyActivationError(string key)
        {
            OnComplete(m_error, m_errors_modify);
            OnEndRequest();
        }

        #endregion IModifyActivation


    }
}